<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属街道" prop="street">
        <el-input
          v-model="queryParams.street"
          placeholder="请输入所属街道"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目地址" prop="productAddress">
        <el-input
          v-model="queryParams.productAddress"
          placeholder="请输入项目地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开工时间" prop="startTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开工时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="建设单位名称" prop="constructUnit">
        <el-input
          v-model="queryParams.constructUnit"
          placeholder="请输入建设单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人姓名" prop="persionName">
        <el-input
          v-model="queryParams.persionName"
          placeholder="请输入负责人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职务" prop="duty">
        <el-input
          v-model="queryParams.duty"
          placeholder="请输入职务"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="劳务分包单位" prop="laborSubcontractId">
        <el-select
              v-model="queryParams.laborSubcontractId"
              placeholder="劳务分包单位"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in subcontracts"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
        <!-- <el-input
          v-model="queryParams.laborSubcontractId"
          placeholder="请输入劳务分包单位id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="专业分包单位" prop="professionalSubcontractingId">
        <el-select
              v-model="queryParams.professionalSubcontractingId"
              placeholder="专业分包单位"
              clearable
              size="small"
              style="width: 240px"
              default-first-option=""
            >
              <el-option
                v-for="dict in professionalSubcontractings"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
        <!-- <el-input
          v-model="queryParams.professionalSubcontractingId"
          placeholder="请输入专业分包单位id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="专业分包单位id" align="center" prop="id" /> -->
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column label="所属街道" align="center" prop="street" />
      <el-table-column label="是否政府投资" align="center" prop="investment" />
      <el-table-column label="项目名称" align="center" prop="productName" />
      <el-table-column label="项目地址" align="center" prop="productAddress" />
      <el-table-column label="开工时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="建设单位名称" align="center" prop="constructUnit" />
      <el-table-column label="负责人姓名" align="center" prop="persionName" />
      <el-table-column label="职务" align="center" prop="duty" />
      <el-table-column label="联系方式" align="center" prop="tel" />
      <el-table-column label="劳务分包单位" align="center" prop="laborSubcontractName" />
      <el-table-column label="专业分包单位" align="center" prop="professionalSubcontractingId" />
      <el-table-column label="总承包单位名称" align="center" prop="generalContractor" />
      <el-table-column label="总承包负责人姓名" align="center" prop="generalUserName" />
      <el-table-column label="总承包职务" align="center" prop="generalDuty" />
      <el-table-column label="总承包联系方式" align="center" prop="generalTel" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.status === '审核失败'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status === '审核失败'"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="agree(scope.row)"
            v-if="roleKey === 'common'&&scope.row.status === '待审核'"
          >同意</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="reject(scope.row)"
            v-if="roleKey === 'common'&&scope.row.status === '待审核'"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="submitAgain(scope.row)"
            v-if="scope.row.status === '审核失败'" 
          >重新提交</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="所属街道" prop="street">
          <el-input v-model="form.street" placeholder="请输入所属街道" />
        </el-form-item>
        <el-form-item label="是否政府投资" prop="investment">
          <el-select
              v-model="form.investment"
              placeholder="是否政府投资"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in goverments"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          <!-- <el-input v-model="form.investment" placeholder="请输入是否政府投资" /> -->
        </el-form-item>
        <el-form-item label="项目名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目地址" prop="productAddress">
          <el-input v-model="form.productAddress" placeholder="请输入项目地址" />
        </el-form-item>
        <el-form-item label="开工时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="建设单位名称" prop="constructUnit">
          <el-input v-model="form.constructUnit" placeholder="请输入建设单位名称" />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="persionName">
          <el-input v-model="form.persionName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="职务" prop="duty">
          <el-input v-model="form.duty" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="劳务分包单位" prop="laborSubcontractId">
          <el-select
              v-model="form.laborSubcontractId"
              placeholder="劳务分包单位"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in subcontracts"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          <!-- <el-input v-model="form.laborSubcontractId" placeholder="请输入劳务分包单位id" /> -->
        </el-form-item>
        <el-form-item label="专业分包单位" prop="professionalSubcontractingId">
          <el-select
              v-model="form.professionalSubcontractingId"
              placeholder="专业分包单位"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in professionalSubcontractings"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select>
          <!-- <el-input v-model="form.professionalSubcontractingId" placeholder="请输入专业分包单位id" /> -->
        </el-form-item>
        <el-form-item label="总承包单位名称" prop="generalContractor">
          <el-input v-model="form.generalContractor" placeholder="总承包单位名称" />
        </el-form-item>
        <el-form-item label="总承包负责人姓名" prop="generalUserName">
          <el-input v-model="form.generalUserName" placeholder="总承包负责人姓名" />
        </el-form-item>
        <el-form-item label="总承包职务" prop="generalDuty">
          <el-input v-model="form.generalDuty" placeholder="总承包职务" />
        </el-form-item>
        <el-form-item label="总承包联系方式" prop="generalTel">
          <el-input v-model="form.generalTel" placeholder="总承包联系方式" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审批意见" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="备注" >
          <el-input v-model="form.remark" type="textarea" placeholder="请输入多行文本"
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rejectSub">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInformation, getInformation, delInformation, addInformation, updateInformation, exportInformation,subcontractList,subcontractingList,agree,submitAgain,reject } from "@/api/system/information";

export default {
  name: "Information",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      open1:false,
      // 总条数
      total: 0,
      // 基本信息表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      roleKey:"",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: "公司",
        street: "街道",
        investment: null,
        productName: null,
        productAddress: null,
        startTime: new Date(),
        constructUnit: null,
        persionName: null,
        duty: null,
        tel: null,
        laborSubcontractId: null,
        professionalSubcontractingId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      professionalSubcontractings:[],
      subcontracts:[],
      goverments:[
        {id:'是',
        name:'是'},
        {id:'否',
        name:'否'}
      ],
      rules1: {
        remark: [
          { required: true, trigger: "blur", message: "请输入审批意见" }
        ]
      },
    };
  },
  created() {
    this.getList(1);
    this.subcontractList();
    this.subcontractingList();
  },
  methods: {
    reject(row){
      this.form.id = row.id;
      this.open1=true;
    },
    cancel1(){
      this.open1=false;
    },
    agree(row){
      this.form.id = row.id;
      agree(this.form).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.getList();
            });
    },
    rejectSub(){
      reject(this.form).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.open1 = false;
              this.getList();
            });
    },
    submitAgain(row){
      this.form.id = row.id;
      submitAgain(this.form).then(response => {
              this.$modal.msgSuccess("重新发起成功");
              this.getList();
            });
    },
    subcontractList(){
      subcontractList().then(response => {
        this.subcontracts = response.data;
        if(response.data.length>0){
          this.queryParams.laborSubcontractId = response.data[0].id;
        }
        
      });
    },
    subcontractingList(){
      subcontractingList().then(response => {
        this.professionalSubcontractings = response.data;
        if(response.data.length>0){
          this.queryParams.professionalSubcontractingId = response.data[0].id;
        }
      });
    },
    /** 查询基本信息列表 */
    getList(num) {
      this.loading = true;
      let queryParams = {}
      if(num===2){
        queryParams = this.queryParams
      }
      listInformation(queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.roleKey = response.rows[0].roleKey
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyName: null,
        street: null,
        investment: null,
        productName: null,
        productAddress: null,
        startTime: null,
        constructUnit: null,
        persionName: null,
        duty: null,
        tel: null,
        laborSubcontractId: null,
        professionalSubcontractingId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(2);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInformation(id).then(response => {
        this.form = response.data;
        this.form.professionalSubcontractingId = parseInt(response.data.professionalSubcontractingId);
        this.open = true;
        this.title = "修改基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除基本信息编号为"' + ids + '"的数据项？').then(function() {
        return delInformation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有基本信息数据项？').then(() => {
        this.exportLoading = true;
        return exportInformation(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
